package com.markerhub.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.entity.Course;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.markerhub.entity.ManagerClass;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


@Mapper
public interface CourseMapper extends BaseMapper<Course> {
    @Select({
            "SELECT COUNT(1) "+
                    "FROM m_course c "+
                    "LEFT JOIN m_manager_activity a ON c.activity_id = a.id "+
                    "LEFT JOIN m_manager_teacher t ON c.teacher_id = t.id "+
                    "WHERE 1 = 1 "+
                    "AND (#{activityName} IS NULL OR a.name LIKE CONCAT('%', #{activityName}, '%')) "+
                    "AND (#{courseName} IS NULL OR c.course_name LIKE CONCAT('%', #{courseName}, '%')) "+
                    "AND (#{status} IS NULL OR c.status LIKE CONCAT('%', #{status}, '%')) "+
                    "AND (#{teacherName} IS NULL OR t.teacher_name LIKE CONCAT('%', #{teacherName}, '%'))"
    })
    Integer countCourses(
            @Param("activityName") String activityName,
            @Param("courseName") String courseName,
            @Param("status") String status,
            @Param("teacherName") String teacherName
    );


    // 自定义分页查询：根据多个条件查询教学班列表
    @Select({
            "SELECT c.*, a.name AS activity_name, t.teacher_name "+
                    "FROM m_course c "+
                    "LEFT JOIN m_manager_activity a ON c.activity_id = a.id "+
                    "LEFT JOIN m_manager_teacher t ON c.teacher_id = t.id "+
                    "WHERE 1 = 1 "+
                    "AND (#{activityName} IS NULL OR a.name LIKE CONCAT('%', #{activityName}, '%')) "+
                    "AND (#{courseName} IS NULL OR c.course_name LIKE CONCAT('%', #{courseName}, '%')) "+
                    "AND (#{status} IS NULL OR c.status LIKE CONCAT('%', #{status}, '%')) "+
                    "AND (#{teacherName} IS NULL OR t.teacher_name LIKE CONCAT('%', #{teacherName}, '%')) "+
                    "ORDER BY c.id DESC"
    })
    IPage<Course> getCourses(Page<Course> page,
                                   @Param("activityName") String activityName,
                                   @Param("courseName") String couseName,
                                   @Param("status") String status,
                                   @Param("teacherName") String teacherName
    );

    @Select("SELECT status, COUNT(*) as count " +
            "FROM m_course " +
            "WHERE teacher_id = #{teacherId} " +
            "GROUP BY status")
    List<Map<String, Object>> selectStatusCount(@Param("teacherId") Long teacherId);

    @Select("SELECT DATE_FORMAT(created_time, '%Y-%m') as month, COUNT(*) as count " +
            "FROM m_course " +
            "WHERE teacher_id = #{teacherId} " +
            "AND created_time >= DATE_SUB(CURDATE(), INTERVAL #{months} MONTH) " +
            "GROUP BY month " +
            "ORDER BY month")
    List<Map<String, Object>> selectMonthlyCount(
            @Param("teacherId") Long teacherId,
            @Param("months") Integer months
    );
}
